<?php

defined('_SECURITY_TOKEN') or die("caia fora");

abstract class application extends framework{
		
	private static $outputview="";
	private static $outputHeaderCSS=array();
	private static $outputHeaderJS=array();
	protected static $_values = array();
	
	public function __construct(){
		
		
		// try {
//         	
			// self::start_session( 'db' );
// 			
			// self::plugin();
// 		
			// self::module();
// 			
			// self::component();
// 			
			// self::template();
// 			
	    // }
	    // catch (AppException $e) {
	    	// throw new AppException( $e->getMessage() );
	        // //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    // }
	    // catch (Exception $e) {
	        // echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    // }
		
					
	}
	
	public static function setAttribute( $varName, $val ){ 
    	self::$_values[ $varName ] = $val; 
	}
	
	public static function getAttribute( $varName ){ 

		try {
        	
			if( !isset( self::$_values[ $varName ] ))
	        	throw new AppException( ATTRIBUTE_NOT_DEFINED.': '.$varName );
	   					
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		
		
	    return self::$_values[ $varName ]; 
	}
	
	
	
	public function instance(){
				
		try {
        	
			self::start_session( 'db' );
			
			self::plugin();
		
			self::module();
			
			self::component();
			
			self::template();
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
			
		
	}
	
	private function plugin(){
		
	}
	
	private function module(){
		
	}
	
	private function component(){
			
		try {
        	
			$component	=	self::getComponent();

			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							."component"
							._SEPARATOR_SO
							.$component
							._SEPARATOR_SO
							.$component
							.'.php';
									
			if( file_exists( $filename ) ){
				
				require_once( PATH_APPLICATION
							._SEPARATOR_SO
							.'controller.class.php' );
					
				require_once( $filename );
						
			}else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
					
							
	}
	
	private function template(){
	
		
		try {
        	
			$webservice=self::getWebservice();
		
			if( empty($webservice) ){
			
				$template	=	self::getTemplate();
	
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."template"
								._SEPARATOR_SO
								.$template
								._SEPARATOR_SO
								.$template
								.'.php';
										
				if( file_exists( $filename ) ){
																	
					require_once( $filename );	
					
				}else
					throw new AppException( FILE_NOT_FOUND.': '.$filename );
				
			}else{
					
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."template"
								._SEPARATOR_SO
								.'default'
								._SEPARATOR_SO
								.'webservice'
								.'.php';
										
				if( file_exists( $filename ) ){
																	
					require_once( $filename );	
					
				}else
					throw new AppException( FILE_NOT_FOUND.': '.$filename );
				
			}
			
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
					
							
	}
	
	public function getOutputController(){
		
		return self::$outputview;
	}
	
	public function executeController(){

		
		try {
        						
			$controller	=	self::getController();
	
			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							."component"
							._SEPARATOR_SO
							.self::getComponent()
							._SEPARATOR_SO
							.'controller'
							._SEPARATOR_SO
							.'controller'
							.self::getController()
							.'.class.php';
												
			if( file_exists( $filename ) ){
								
				require_once(  $filename );
				
				$controllername	=	'controller'
									.self::getController();
							
				$controller 	= new $controllername( );
														
			}else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		
									
	}
	
	
	public function executeView( $view="" ){

		try {
        						
			if( !empty($view) ){
			
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."component"
								._SEPARATOR_SO
								.self::getComponent()
								._SEPARATOR_SO
								.'view'
								._SEPARATOR_SO
								.'view'
								.$view
								.'.class.php';
													
				if( file_exists( $filename ) ){
									
					require_once(  $filename );
					
					$viewname	=	'view'.$view;
								
					$viewname	= new $viewname( );
															
				}else
					throw new AppException( FILE_NOT_FOUND.': '.$filename );
			
			}else
				throw new AppException( VIEW_NOT_DEFINED.': '.$filename );
	
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		
		
	}
	
	
	public function setViewTmlp( $tmpl="default" ){

		try {
        									
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."component"
								._SEPARATOR_SO
								.self::getComponent()
								._SEPARATOR_SO
								.'view'
								._SEPARATOR_SO
								.'tmpl'
								._SEPARATOR_SO
								.$tmpl
								.'.php';
													
				if( file_exists( $filename ) ){
					
					ob_start();
					require_once(  $filename );	
					
					self::$outputview = ob_get_contents();
										
					//ob_end_flush();	
					ob_end_clean();		
												 
																		
				}else
					throw new AppException( TMPL_NOT_DEFINED.': '.$filename );
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		
		
	}
	
	
	public function getWebservice(){
		
		$result=NULL;
		
		try {
        	
			if( !empty($_REQUEST['webservice']) )
				if( $_REQUEST['webservice']!='' )
					$result	=	$_REQUEST['webservice'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}
	
	
	
	public function getTemplate(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['tmpl']) ){
				
				if( empty( configuration::$template_default) )
					throw new AppException( TEMPLATE_NOT_DEFINED );
				else
					$result	=	configuration::$template_default;
			}
			else
				$result	=	$_REQUEST['tmpl'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}
	
	public function getComponent(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['component']) )
				$result	=	'system';//throw new AppException( COMPONENT_NOT_DEFINED );
			else
				$result	=	$_REQUEST['component'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}
	
	public function getController(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['controller']) )
				$result	=	'system';//throw new AppException( CONTROLLER_NOT_DEFINED );
			else
				$result	=	$_REQUEST['controller'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}

	
	public function getTask(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['task']) )
				throw new AppException( TASK_NOT_DEFINED );
			else
				$result	=	$_REQUEST['task'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}



	public function IsAuthenticationRequired( $required=FALSE, $redirect="" ){
		
		if( $required == TRUE){
			
			if( !self::getAuthentication() ){
				
				header("Location: index.php"
							.'?component='
							.'login'
							.'&controller='
							.'login'
							.'&task='
							.'restrict'
							.'&tmpl=default'
							, TRUE, 301); 
			
			}
		}
				
	}
	
	public function clearHeaderCSS(){
		
		try {
        	
			self::$outputHeaderCSS = array();
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
	}
	
	public function addHeaderCSS( $url="", $rel="stylesheet"){
		
		try {
        	
			array_push( self::$outputHeaderCSS, array('url'=>$url,'rel'=>$rel));
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
		
	}
	
	public function getHeaderCSS(){
			
		$result='';
		
		try {
        	
			foreach( self::$outputHeaderCSS as $key=>$item ){

				$result .= '<link rel="'.$item['rel'].'" href="'.$item['url'].'" type="text/css" />'."\n";
			
			}						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
		
	}
	
	
	public function clearHeaderJS(){
		
		try {
        	
			self::$outputHeaderJS = array();
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
	}
	
	public function addHeaderJS( $url=""){
		
		try {
        	
			array_push( self::$outputHeaderJS, array('url'=>$url));
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
		
	}
	
	public function getHeaderJS(){
			
		$result='';
		
		try {
        	
			foreach( self::$outputHeaderJS as $key=>$item )				
				$result .= '<script src="'.$item['url'].'"></script>'."\n";
									
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
		
	}
	
	
	public function getModel( $model="" ){
	
		$result	=	false;
		
		try {
        	
			$component	=	self::getComponent();

			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							."component"
							._SEPARATOR_SO
							.$component
							._SEPARATOR_SO
							.'model'
							._SEPARATOR_SO
							.'model'
							.$model
							.'.class.php';
									
			if( file_exists( $filename ) ){
				
				self::instanceModel();
				
				require_once( $filename );
				
				$modelname	=	'model'.$model;
								
				$result	= new $modelname( );
					
			}	
			else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
			
					
		return $result;
									
	}
	
	
	private function instanceModel( ){
		
		try {

			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							.'model'
							.'.class.php';
									
			if( file_exists( $filename ) ){
				
				require_once( $filename );

			}	
			else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
			
		
									
	}
	
	
	
}

?>